<?php

namespace App\Http\Controllers\Assets;

use App\Http\Controllers\Controller;
use App\Models\Assets\PortfolioSnapshotsModel;
use App\Models\Assets\RevenuesByDayModel;
use App\Models\Assets\RevenuesModel;
use App\Service\Assets\AssetsService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 收入
 */
class RevenuesController extends Controller
{
    // 投资组合概览
    public function overview(Request $request)
    {
        // 默认获取的是昨日的 也就是最新的一条 按照ID 降序
        $portfolioSnapshotsModel = PortfolioSnapshotsModel::query()
            ->where('user_id', $request->user_id)
            ->orderByDesc('id')
            ->first();

        $data = [
            'total_value' => $portfolioSnapshotsModel ? number_format($portfolioSnapshotsModel->total_value / 100, 2, '.', '') : 0,
            'value'       => $portfolioSnapshotsModel ? number_format($portfolioSnapshotsModel->value / 100, 2, '.', '') : 0,
        ];

        return $this->success($data);
    }

    /**
     * 修改总盈亏
     * @param Request $request
     * @return JsonResponse
     */
    public function updateTotalAmount(Request $request)
    {
        $amount = $request->post('amount', 0);

        $portfolioSnapshotsModel = PortfolioSnapshotsModel::query()
            ->where('user_id', $request->user_id)
            ->first();

        if (!$portfolioSnapshotsModel) {
            return $this->validationError("非法用户");
        }

        $portfolioSnapshotsModel->total_value = $portfolioSnapshotsModel->total_value + ($amount * 100);
        $portfolioSnapshotsModel->save();

        return $this->success();
    }

    public function revenueTrend(Request $request)
    {
        $days = $request->get('days', 10);

        $trends = RevenuesByDayModel::query()
            ->orderByDesc('id')
            ->where('user_id', $request->user_id)
            ->limit($days)
            ->get();

        // 汇总今天的总额
        $todayAmount = AssetsService::revenues($request->user_id);

        $formattedTrends = $trends->map(function ($item) {
            $item->amount = number_format($item->amount / 100, 2, '.', '');
            return $item;
        });


        return $this->success([
            'today'   => number_format($todayAmount / 100, 2, '.', ''),
            'list'    => $formattedTrends->toArray(),
            'total'   => $formattedTrends->sum('amount'),
            'average' => number_format($formattedTrends->avg('amount'), 2, '.', '')
        ]);
    }

    public function revenueAdd(Request $request)
    {
        $amount            = $request->post('amount', 0);
        $revenues          = new RevenuesModel();
        $revenues->user_id = $request->user_id;
        $revenues->amount  = $amount * 100;
        $revenues->save();

        // 修改总盈亏字段
        $portfolioSnapshotsModel = PortfolioSnapshotsModel::query()
            ->updateOrCreate([
                'user_id' => $request->user_id,
            ]);

        $portfolioSnapshotsModel->total_value = $portfolioSnapshotsModel->total_value + $amount * 100;
        $portfolioSnapshotsModel->save();

        return $this->success();
    }

}
